using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ClownFish
{
	public static class XmlHelper
	{
		private static void smethod_0(Stream stream_0, object object_0, Encoding encoding_0)
		{
			if (object_0 == null)
			{
				throw new ArgumentNullException("o");
			}
			if (encoding_0 == null)
			{
				throw new ArgumentNullException("encoding");
			}
			XmlSerializer xmlSerializer = new XmlSerializer(object_0.GetType());
			using (XmlWriter xmlWriter = XmlWriter.Create(stream_0, new XmlWriterSettings
			{
				Indent = true,
				NewLineChars = "\r\n",
				Encoding = encoding_0,
				IndentChars = "    "
			}))
			{
				xmlSerializer.Serialize(xmlWriter, object_0);
				xmlWriter.Close();
			}
		}

		public static string XmlSerialize(object object_0, Encoding encoding)
		{
			string result;
			using (MemoryStream memoryStream = new MemoryStream())
			{
				XmlHelper.smethod_0(memoryStream, object_0, encoding);
				memoryStream.Position = 0L;
				using (StreamReader streamReader = new StreamReader(memoryStream, encoding))
				{
					result = streamReader.ReadToEnd();
				}
			}
			return result;
		}

		public static void XmlSerializeToFile(object object_0, string path, Encoding encoding)
		{
			if (string.IsNullOrEmpty(path))
			{
				throw new ArgumentNullException("path");
			}
			using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
			{
				XmlHelper.smethod_0(fileStream, object_0, encoding);
			}
		}

		public static T XmlDeserialize<T>(string string_0, Encoding encoding)
		{
			if (string.IsNullOrEmpty(string_0))
			{
				throw new ArgumentNullException("s");
			}
			if (encoding == null)
			{
				throw new ArgumentNullException("encoding");
			}
			XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
			T result;
			using (MemoryStream memoryStream = new MemoryStream(encoding.GetBytes(string_0)))
			{
				using (StreamReader streamReader = new StreamReader(memoryStream, encoding))
				{
					result = (T)((object)xmlSerializer.Deserialize(streamReader));
				}
			}
			return result;
		}

		public static T XmlDeserializeFromFile<T>(string path, Encoding encoding)
		{
			if (string.IsNullOrEmpty(path))
			{
				throw new ArgumentNullException("path");
			}
			if (encoding == null)
			{
				throw new ArgumentNullException("encoding");
			}
			string string_ = File.ReadAllText(path, encoding);
			return XmlHelper.XmlDeserialize<T>(string_, encoding);
		}

		public static string XmlSerializerObject(object object_0)
		{
			if (object_0 == null)
			{
				throw new ArgumentNullException("o");
			}
			Encoding uTF = Encoding.UTF8;
			XmlSerializer xmlSerializer = new XmlSerializer(object_0.GetType());
			string result;
			using (MemoryStream memoryStream = new MemoryStream())
			{
				XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
				xmlWriterSettings.Indent = true;
				xmlWriterSettings.NewLineChars = "\r\n";
				xmlWriterSettings.Encoding = uTF;
				xmlWriterSettings.OmitXmlDeclaration = true;
				xmlWriterSettings.IndentChars = "    ";
				XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();
				xmlSerializerNamespaces.Add("", "");
				using (XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings))
				{
					xmlSerializer.Serialize(xmlWriter, object_0, xmlSerializerNamespaces);
					xmlWriter.Close();
				}
				memoryStream.Position = 0L;
				using (StreamReader streamReader = new StreamReader(memoryStream, uTF))
				{
					result = streamReader.ReadToEnd();
				}
			}
			return result;
		}
	}
}
